﻿using System;
using System.Linq;
using System.Collections.Generic;
using BusinessObjects;
using DataObjects.EntityFramework.ModelMapper;
using System.Linq.Dynamic;

namespace DataObjects.EntityFramework.Implementation
{
    /// <summary>
    /// Entity Framework implementation of the IOrderDetailDao interface.
    /// </summary>
    public class EntityOrderDetailDao : IOrderDetailDao
    {
        /// <summary>
        /// Gets the orderdetails for a given order.
        /// </summary>
        /// <param name="orderId">Unique order identifier.</param>
        /// <returns>List of orderdetails.</returns>
        public List<OrderDetail> GetOrderDetails(int orderId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var orderDetails = context.OrderDetailEntities.Where(d => d.OrderId == orderId).ToList();

                var list = new List<OrderDetail>();
                foreach (var orderDetail in orderDetails)
                    list.Add(Mapper.Map(orderDetail));

                return list;
            }
        }
        public List<OrderDetail> GetOrderDetails(string sortExpression)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var list = new List<OrderDetail>();

                //var customers = context.CustomerEntities.AsQueryable().OrderBy(sortExpression).ToList();
                var orderDetails = context.OrderDetailEntities.AsQueryable().OrderBy(sortExpression).ToList();
                foreach (var orderDetail in orderDetails)
                    list.Add(Mapper.Map(orderDetail));

                return list;
            }
        }
        public OrderDetail GetOrderDetail(int orderDetailId)
        {
            return null;
        }
        public void InsertOrderDetail(OrderDetail orderDetail)
        {
            var entity = Mapper.Map(orderDetail);
            
            using (var context = DataObjectFactory.CreateContext())
            {
                context.OrderDetailEntities.AddObject(entity);
                context.SaveChanges();

                // update business object with new version and id
                orderDetail.OrderId = entity.OrderId;

                //country.Version = Convert.ToBase64String(entity.Version);
            }
        }
        public void UpdateOrderDetail(OrderDetail orderDetail)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.OrderDetailEntities.Where(o => o.OrderId == orderDetail.OrderId).SingleOrDefault();

                entity.OrderId = orderDetail.OrderId;
                entity.CategoryId = orderDetail.CategoryId;
                entity.ProductId = orderDetail.ProductId;
                entity.UnitPrice = orderDetail.UnitPrice;
                entity.Quantity = orderDetail.Quantity;
                entity.Discount = orderDetail.Discount;
                entity.Status = orderDetail.Status;

                context.OrderDetailEntities.ApplyCurrentValues(entity);
                context.SaveChanges();

                // Update business object with new version
                orderDetail.Version = Convert.ToBase64String(entity.Version);
            }
        }

        public void UpdateOrderDetailStatus(OrderDetail orderDetail)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.OrderDetailEntities.Where(o => o.OrderId == orderDetail.OrderId && o.ProductId == orderDetail.ProductId).SingleOrDefault();

                entity.Status = orderDetail.Status;

                context.OrderDetailEntities.ApplyCurrentValues(entity);
                context.SaveChanges();

                // Update business object with new version
                orderDetail.Version = Convert.ToBase64String(entity.Version);
            }
        }

        public void DeleteOrderDetail(OrderDetail orderDetail)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.OrderDetailEntities.Where(o => o.OrderId == orderDetail.OrderId).SingleOrDefault();
                context.OrderDetailEntities.DeleteObject(entity);
                context.SaveChanges();
            }

        }
        public void DeleteOrderDetailByOrderId(int OrderId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.OrderDetailEntities.Where(o => o.OrderId == OrderId).SingleOrDefault();
                context.OrderDetailEntities.DeleteObject(entity);
                context.SaveChanges();
            }

        }
    }
}
